<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AdminLogin</title>
    <script src="/static/jquery-3.6.0.min.js"></script>
    <script src="/static/md5.js"></script>
</head>
<body>
<div>
    用户名:<input name="username" type="text"><br>
    密 码 :<input name="password" type="password"><br>
    <input name="rememberMe" type="checkbox" checked="checked">记住我<br>
    <button type="button" id="adminLogin">登录</button>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" id="adminRegister">点击注册</button>
</div>
<script th:inline="javascript">
    $(function () {
        adminRegister();
        //首先查看用户是否登录过，如果登录过，则自动填写帐号和密码
        let cookieData = JSON.parse([[${resData}]]);
        //用户名自动回显
        if (cookieData.resCode == 200){
            $("[name=username]").val(cookieData.resObj.username);
            $("[name=password]").val(cookieData.resObj.password);
        }
        gotoLogin(cookieData);
    });
    function adminRegister(){
        $("#adminRegister").click(function (){
            location.href="/admin/register"
        });
    }
    function gotoLogin(cookieData){
        $("#adminLogin").click(function () {
            let username = $("[name=username]").val();
            let password = $("[name=password]").val();
            //当前密码与后端回显密码不相同时，对密码进行md5加密（此种情况下表示用户自动输入了密码）
            if (password != cookieData.resObj.password && password != ""){
                password = hex_md5(password);
            }
            let rememberMe = $("[name=rememberMe]").prop("checked");
            $.post("/admin/adminLogin?t=" + new Date().getTime(), {
                "username": username,
                "password": password,
                "rememberMe": rememberMe
            }, function (data) {
                let resData = JSON.parse(data);
                console.log(resData);
                if (resData.resCode == 200){
                    alert(resData.resMsg);
                    location.href="/admin/index";
                } else {
                    alert(resData.resMsg);
                }
            });
        });
    }
</script>
</body>
</html>